package handler

import (
	"api/common"
	"api/internal/request"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
	pb "srv/proto"
)

func DoctorRegister(c *gin.Context) {
	json := request.DoctorRegisterRequest{}
	err := c.ShouldBindJSON(&json)
	if err != nil {
		common.JsonRes(c, 400, nil, "请求参数不正确")
		return
	}
	conn, err := grpc.Dial("127.0.0.1:50051", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		zap.S().Error(err)
	}
	defer conn.Close()
	con := pb.NewUserClient(conn)
	req := &pb.DoctorRegisterReq{
		Name:          json.Name,
		Image:         json.Image,
		Hospital:      json.Hospital,
		Root:          json.Room,
		Nickname:      json.Nickname,
		Type:          int64(json.Type),
		RegisterPrice: float32(json.RegisterPrice),
		InspectPrice:  float32(json.InspectPrice),
		Intro:         json.Intro,
	}
	register, err := con.DoctorRegister(c, req)
	if err != nil {
		common.JsonRes(c, 400, nil, err.Error())
		return
	}
	common.JsonRes(c, 200, register, "注册成功")
}

func DoctorList(c *gin.Context) {
	conn, err := grpc.Dial("127.0.0.1:50051", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		zap.S().Error(err)
	}
	defer conn.Close()
	con := pb.NewUserClient(conn)
	req := &pb.DoctorListReq{}
	register, err := con.DoctorList(c, req)
	if err != nil {
		common.JsonRes(c, 400, nil, err.Error())
		return
	}
	common.JsonRes(c, 200, register, "医生展示成功")
}
func MessageCreate(c *gin.Context) {
	json := request.MessageCreateRequest{}
	err := c.ShouldBindJSON(&json)
	if err != nil {
		common.JsonRes(c, 400, nil, "请求参数不正确")
		return
	}
	conn, err := grpc.Dial("127.0.0.1:50051", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		zap.S().Error(err)
	}
	defer conn.Close()
	con := pb.NewUserClient(conn)
	req := &pb.MessageCreateReq{
		Name:            json.Name,
		IdCardType:      int64(json.IdCardType),
		WXName:          json.WXName,
		Address:         json.Address,
		MedicalIdCard:   json.MedicalIdCard,
		IdCard:          json.IdCard,
		Image:           json.Image,
		Mobile:          json.Mobile,
		Relationship:    int64(json.Relationship),
		Hospital:        json.Hospital,
		HospitalAddress: json.HospitalAddress,
		Room:            json.Room,
		DoctorName:      json.DoctorName,
		Nickname:        json.Nickname,
		Type:            int64(json.Type),
		Time:            json.Time,
		WaitTime:        json.WaitTime,
		RegisterPrice:   float32(json.RegisterPrice),
		CreateTime:      json.CreateTime,
		Status:          int64(json.Status),
		Cancel:          json.Cancel,
		CancelTime:      json.CancelTime,
		Comment:         json.Comment,
	}
	register, err := con.MessageCreate(c, req)
	if err != nil {
		common.JsonRes(c, 400, nil, err.Error())
		return
	}
	common.JsonRes(c, 200, register, "预约成功")
}

func MessageShow(c *gin.Context) {
	json := request.MessageShowRequest{}
	err := c.ShouldBindJSON(&json)
	if err != nil {
		common.JsonRes(c, 400, nil, "请求参数不正确")
		return
	}
	conn, err := grpc.Dial("127.0.0.1:50051", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		zap.S().Error(err)
	}
	defer conn.Close()
	con := pb.NewUserClient(conn)
	req := &pb.MessageShowReq{
		Id: int64(json.Id),
	}
	register, err := con.MessageShow(c, req)
	if err != nil {
		common.JsonRes(c, 400, nil, err.Error())
		return
	}
	common.JsonRes(c, 200, register, "预约信息展示成功")
}

func MessageUpdate(c *gin.Context) {
	json := request.MessageUpdateRequest{}
	err := c.ShouldBindJSON(&json)
	if err != nil {
		common.JsonRes(c, 400, nil, "请求参数不正确")
		return
	}
	conn, err := grpc.Dial("127.0.0.1:50051", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		zap.S().Error(err)
	}
	defer conn.Close()
	con := pb.NewUserClient(conn)
	req := &pb.MessageUpdateReq{
		Cancel: json.Cancel,
		Id:     int64(json.Id),
	}
	register, err := con.MessageUpdate(c, req)
	if err != nil {
		common.JsonRes(c, 400, nil, err.Error())
		return
	}
	common.JsonRes(c, 200, register, "预约信息修改成功")
}
